var userAgent = navigator.userAgent;
var isOpera = userAgent.indexOf("Opera") > -1;
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;
var isFF = userAgent.indexOf("Firefox") > -1;
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") < 1;
var isChrome = userAgent.indexOf("Chrome") > -1;
var ownerMap = new Array();
var loadedHandlers = new Array();
var customPropertyMap = new Array();
var customEventHandlers = new Array();
var focusedElementId = "";

var pageIndices = null;
var currentPageIndex = -1;
var realCurPageIndex = -1;
var pageCount = -1;

function switchPage(targetIndex) {
	if (pageIndices == null) {
		pageIndices = document.body.getAttribute("pages").split(",");
	}
	if (targetIndex >= 0 && (pageCount == -1 || targetIndex < pageCount)) {
		resetPages();
		pageCount = pageIndices.length;
		realCurPageIndex = parseInt(pageIndices[targetIndex], 10);
		if (showPage(realCurPageIndex)) {
			currentPageIndex = targetIndex;
			var pageDiv = document.getElementById("Page" + (realCurPageIndex + 1));
			if (pageDiv != null) {
				window.status = "Page (" + (targetIndex + 1) + " / " + pageCount + ") : " + pageDiv.getAttribute("pageTitle");
			}
		}
	}
}

function resetPages() {
	var index = 0;
	while(true) {
		var pageDiv = document.getElementById("Page" + (index + 1));
		if (pageDiv == null) {
			break;
		} else {
			pageDiv.style.visibility = "hidden";
			pageDiv.parentNode.removeChild(pageDiv);
			document.body.appendChild(pageDiv);
			index ++;
		}
	}
	return index;
}

function showPage(targetIndex) {
	var pageDiv = document.getElementById("Page" + (targetIndex + 1));
	if (pageDiv != null) {
		pageDiv.style.visibility = "visible";
		var masterPages = pageDiv.getAttribute("masterPages");
		if (masterPages != "") {
			var baseChild = pageDiv.childNodes[0];
			masters = masterPages.split(",");
			for (i = 0; i < masters.length; i ++) {
				var masterDiv = document.getElementById(masters[i]);
				masterDiv.style.visibility = "visible";
				masterDiv.parentNode.removeChild(masterDiv);
				if (baseChild == null) {
					pageDiv.appendChild(masterDiv);
				} else {
					pageDiv.insertBefore(masterDiv, baseChild);
				}
			}
		}
		setTimeout("try{eval('" + pageDiv.id + "_onPageLoaded();');}catch(err){}", 200);
		return true;
	}
	return false;
}

function goToPage(internalIndex) {
	if (pageIndices == null) {
		pageIndices = document.body.getAttribute("pages").split(",");
	}
	var targetIndex = -1;
	for (var i = 0; i < pageIndices.length; i ++) {
		if (parseInt(pageIndices[i]) == internalIndex) {
			targetIndex = i;
			break;
		}
	}
	if (targetIndex != -1) {
		switchPage(targetIndex);
	} else {
		var tarPageDiv = document.getElementById("Page" + (internalIndex + 1));
		if (tarPageDiv != null) {
			resetPages();
			showPage(internalIndex);
		}
	}
}

function addHandler(obj, ev, func) {
	if (window.attachEvent) {
		obj.attachEvent("on" + ev, func);
	} else {
		obj.addEventListener(ev, func, false);
	}
}

function invokeAfterLoaded(func) {
	loadedHandlers.push(func);
}

function invokeLoadedHandlers() {
	for (var i = 0; i < loadedHandlers.length; i ++) {
		loadedHandlers[i].call(window);
	}
}

function getPosLeft(obj) { 
	var l = obj.offsetLeft; 
	while(obj = obj.offsetParent) { 
		l += obj.offsetLeft; 
	} 
	return l; 
}

function getPosTop(obj) { 
	var t = obj.offsetTop; 
	while(obj = obj.offsetParent) { 
		t += obj.offsetTop; 
	} 
	return t; 
}

function getPageByElement(elem) {
	var obj = elem;
	while (obj = obj.parentNode) {
		if (obj.id.indexOf("Page") == 0) {
			return obj;
		}
	}
	return null;
}

function ptInRect(x, y, l, t, w, h) {
	return (x >= l) && (x < (l + w)) && (y >= t) && (y < (t + h));   
}

function rectInRect(l, t, w, h, bL, bT, bW, bH) {
	var ltIn = ptInRect(l, t, bL, bT, bW, bH);
	var lbIn = ptInRect(l, t + h - 1, bL, bT, bW, bH);
	var rtIn = ptInRect(l + w - 1, t, bL, bT, bW, bH);
	var rbIn = ptInRect(l + w - 1, t + h - 1, bL, bT, bW, bH);
	return ltIn && lbIn && rtIn && rbIn;
}

function arrayContains(arr, obj) {
	var i = arr.length;
	while (i--) {
		if (arr[i] === obj) {
			return true;
		}
	}
	return false;
}

function saveOwner(child, owner) {
	if (child.id == null || child.id == "") {
		return false;
	}
	for (var id in ownerMap) {
		if (ownerMap[id] != null && ownerMap[id].children != null && arrayContains(ownerMap[id].children, child.id)) {
			return false;
		}
	}
	if (typeof(ownerMap[owner.id]) == "undefined") {
		ownerMap[owner.id] = new Object();
		ownerMap[owner.id].done = false;
		ownerMap[owner.id].children = new Array();
	}
	ownerMap[owner.id].children.push(child.id);
	return true;
}

function changeVisibilityInRect(node, l, t, w, h, visible, excludedElement) {
	if (typeof(ownerMap[excludedElement.id]) == "undefined") {
		ownerMap[excludedElement.id] = new Object();
		ownerMap[excludedElement.id].done = false;
		ownerMap[excludedElement.id].children = new Array();
	}
	if (ownerMap[excludedElement.id].done == true) {
		for (var index in ownerMap[excludedElement.id].children) {
			var child = document.getElementById(ownerMap[excludedElement.id].children[index]);
			if (child != null) {
				var newState = (child.style.visibility == "inherit") ? "hidden" : "inherit";
				if (visible == 0) {
					newState = "hidden";
				} else if (visible == 1) {
					newState = "inherit";
				}
				child.style.visibility = newState;
				if (newState == "hidden") {
					try {
						eval(children[i].id + "_onHidden();");
					} catch (err) {}
				}
			}
		}
	} else {
		var children = node.childNodes;
		for(var i = 0; i < children.length; i ++) {
			if (children[i] != excludedElement && canElemACoverElemB(children[i], excludedElement)) {
				var top = getPosTop(children[i]);
				var left = getPosLeft(children[i]);
				var width = children[i].offsetWidth;
				var height = children[i].offsetHeight;
				if (rectInRect(left, top, width, height, l, t, w, h)) {
					if (saveOwner(children[i], excludedElement)) {
						var newState = (children[i].style.visibility == "inherit") ? "hidden" : "inherit";
						if (visible == 0) {
							newState = "hidden";
						} else if (visible == 1) {
							newState = "inherit";
						}
						children[i].style.visibility = newState;
						if (newState == "hidden") {
							try {
								eval(children[i].id + "_onHidden();");
							} catch (err) {}
						}
					}
				}
				changeVisibilityInRect(children[i], l, t, w, h, visible, excludedElement);
			}
		}
	}
}

function changeVisibility(targetElementId, visible, includeChildren) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		var newState = "inherit";
		if (visible == 0) {
			newState = "hidden";
		} else if (visible == 1) {
			newState = "inherit";
		} else {
			newState = (targetElement.style.visibility == "inherit") ? "hidden" : "inherit";
		}
		targetElement.style.visibility = newState;
		changeInternalElementsVisibility(targetElement, newState, targetElement);
		if (newState == "hidden") {
			try {
				eval(targetElementId + "_onHidden();");
			} catch (err) {}
		}
		if (includeChildren == true) {
			var baseTop = getPosTop(targetElement);
			var baseLeft = getPosLeft(targetElement);
			var baseWidth = targetElement.offsetWidth;
			var baseHeight = targetElement.offsetHeight;
			var ownerPage = getPageByElement(targetElement);
			if (ownerPage != null) {
				changeVisibilityInRect(ownerPage, baseLeft, baseTop, baseWidth, baseHeight, visible, targetElement);
				ownerMap[targetElement.id].done = true;
			}
		}
	}
}

function changeInternalElementsVisibility(parentElement, newState, baseElement) {
	var children = parentElement.childNodes;
	for(var i = 0; i < children.length; i ++) {
		if (isInternalElement(children[i], baseElement)) {
			children[i].style.visibility = newState;
		}
		changeInternalElementsVisibility(children[i], newState, baseElement);
	}
}

function moveElementToPos(targetElement, newX, newY) {
	var parentElement = targetElement.parentNode;
	var baseX = (parentElement == null) ? 0 : getPosLeft(parentElement);
	var baseY = (parentElement == null) ? 0 : getPosTop(parentElement);
	targetElement.style.left = newX - baseX;
	targetElement.style.top = newY - baseY;
	try {
		eval(getElementTypeById(targetElement.id) + "_onMoved('" + targetElement.id + "');");
	} catch (err) {}
}

function synchronizePlaceholders(elem) {
	if (elem != null) {
		for (var i = 0; i < elem.childNodes.length; i ++) {
			var child = elem.childNodes[i];
			try {
				if(child.tagName.toUpperCase() == "SPAN") {
					var tarId = child.getAttribute("peer");
					if (tarId != null) {
						var tarElem = document.getElementById(tarId);
						if (tarElem != null) {
							var parentElement = tarElem.parentNode;
							var baseX = (parentElement == null) ? 0 : getPosLeft(parentElement);
							var baseY = (parentElement == null) ? 0 : getPosTop(parentElement);
							var deltaX = getPosLeft(child) - getPosLeft(tarElem);
							var deltaY = getPosTop(child) - getPosTop(tarElem);
							if (deltaX != 0 || deltaY != 0) {
								var newX = baseX + deltaX;
								var newY = baseY + deltaY;
								moveElementToPos(tarElem, newX, newY);
							}
						}
					}
				}
			} catch (err) {}
			synchronizePlaceholders(child);
		}
	}
}

function findAncestor(node, func) {
	if (node != null && node.parentNode != null) {
		if (func(node.parentNode) == true) {
			return node.parentNode;
		} else {
			return findAncestor(node.parentNode, func);
		}
	}
	return null;
}

function findAllAncestors(node, func, array) {
	if (array == null) {
		array = new Array();
	}
	if (node != null && node.parentNode != null) {
		if (func(node.parentNode) == true) {
			array.push(node.parentNode);
		}
		findAllAncestors(node.parentNode, func, array);
	}
	return array;
}

function findDescendant(node, func) {
	if (node != null) {
		for (var i = 0; i < node.childNodes.length; i ++) {             
			var child = node.childNodes[i];
			if (func(child) == true) {
				return child;
			}
			var childResult = findDescendant(child, func);
			if (childResult != null) {
				return childResult;
			}
		}
	}
	return null;
}

function findAllDescendants(node, func, array, dig) {
	if (array == null) {
		array = new Array();
	}
	if (node != null) {
		for (var i = 0; i < node.childNodes.length; i ++) {             
			var child = node.childNodes[i];
			if (func(child) == true) {
				array.push(child);
			} else if (!dig) {
				findAllDescendants(child, func, array, dig);
			}
			if (dig) {
				findAllDescendants(child, func, array, dig);
			}
		}
	}
	return array;
}

function clearAllChildNodes(parentNode) {
	while (parentNode.firstChild) {
		var oldNode = parentNode.removeChild(parentNode.firstChild);
		oldNode = null;
    }
}

function getChildIndex(parentNode, node) {
	for (var i = 0; i < node.parentNode.childNodes.length; i ++){             
		if(node.parentNode.childNodes[i] == node) {
			return i;
		}
	}
}

function isPageElement(elem) {
	return (elem != null && elem.id != null && elem.id.indexOf("Page") == 0);
}

function isGroupElement(elem) {
	return (elem != null && elem.id != null && elem.id.indexOf("Group_") == 0);
}

function getActualZ(elem) {
	try {
		var z = elem.style.zIndex == "auto" ? 0 : elem.style.zIndex;
	} catch (err) {
		var z = 0;
	}
	if (typeof(z) != "number") {
		z = parseInt(z, 10);
	}
	return z;
}

function canElemACoverElemB(elemA, elemB) {
	var hostA = isPageElement(elemA.parentNode) ? elemA : elemA.parentNode;
	var hostB = isPageElement(elemB.parentNode) ? elemB : elemB.parentNode;
	var nextA = isPageElement(hostA.parentNode) ? hostA : hostA.parentNode;
	var nextB = isPageElement(hostB.parentNode) ? hostB : hostB.parentNode;
	if (nextA != nextB && (nextA != hostA || nextB != hostB)) {
		return canElemACoverElemB(nextA, nextB);
	} else {
		var aIndex = getActualZ(hostA);
		var bIndex = getActualZ(hostB);
		if (aIndex == bIndex && hostA.parentNode == hostB.parentNode) {
			aIndex = getChildIndex(hostA.parentNode, hostA);
			bIndex = getChildIndex(hostB.parentNode, hostB);
		}
		return (aIndex >= bIndex);
	}
}

function isInternalElement(child, host) {
	try {
		return (child.id.indexOf(host.id + "_") == 0);
	} catch (err) {
		return false;
	}
}

function getDraggableElementId(id) {
	var elem = document.getElementById(id);
	if (elem != null) {
		var p = elem.parentNode;
		var c = elem;
		while (p != null) {
			if (isPageElement(p)) {
				return c.id;
			}
			c = p;
			p = p.parentNode;
		}
	}
	return null;
}

function changeLocationInRect(node, l, t, w, h, offsetX, offsetY, excludedElement) {
	if (typeof(ownerMap[excludedElement.id]) == "undefined") {
		ownerMap[excludedElement.id] = new Object();
		ownerMap[excludedElement.id].done = false;
		ownerMap[excludedElement.id].children = new Array();
	}
	if (ownerMap[excludedElement.id].done == true) {
		for (var index in ownerMap[excludedElement.id].children) {
			var child = document.getElementById(ownerMap[excludedElement.id].children[index]);
			if (child != null && !isInternalElement(child, child.parentNode)) {
				var top = getPosTop(child);
				var left = getPosLeft(child);
				if (!isGroupElement(child.parentNode)) {
					moveElementToPos(child, left + offsetX, top + offsetY);
				}
			}
		}
	} else {
		var children = node.childNodes;
		for(var i = 0; i < children.length; i ++) {
			if (children[i] != excludedElement 
				&& canElemACoverElemB(children[i], excludedElement)) {
				var top = getPosTop(children[i]);
				var left = getPosLeft(children[i]);
				var width = children[i].offsetWidth;
				var height = children[i].offsetHeight;
				if (rectInRect(left, top, width, height, l, t, w, h)) {
					if (saveOwner(children[i], excludedElement) && !isInternalElement(children[i], node)) {
						if (!isGroupElement(node)) {
							moveElementToPos(children[i], left + offsetX, top + offsetY);
						}
					}
				}
				changeLocationInRect(children[i], l, t, w, h, offsetX, offsetY, excludedElement);
			}
		}
	}
}

function changeLocation(targetElementId, newX, newY, includeChildren) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		var baseTop = getPosTop(targetElement);
		var baseLeft = getPosLeft(targetElement);
		var baseWidth = targetElement.offsetWidth;
		var baseHeight = targetElement.offsetHeight;
		var ownerPage = getPageByElement(targetElement);
		moveElementToPos(targetElement, newX, newY);
		if (includeChildren == true) {
			if (ownerPage != null) {
				var offsetX = getPosLeft(targetElement) - baseLeft;
				var offsetY = getPosTop(targetElement) - baseTop;
				changeLocationInRect(ownerPage, baseLeft, baseTop, baseWidth, baseHeight, offsetX, offsetY, targetElement);
				ownerMap[targetElement.id].done = true;
			}
		}
	}
}

function getId(id) {
	return id;
}

function getX(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return getPosLeft(obj);
	}
	return -1;
}

function getY(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return getPosTop(obj);
	}
	return -1;
}

function getWidth(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return obj.offsetWidth;
	}
	return -1;
}

function getHeight(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return obj.offsetHeight;
	}
	return -1;
}

function getVisible(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		return obj.style.visibility != "hidden";
	}
	return false;
}

function isShown(obj) {
	if (obj != null) {
		if (obj.style.visibility == "hidden" || obj.style.display == "none") {
			return false;
		} else if (obj.parentNode != null && (typeof(obj.parentNode.id) != "string" || obj.parentNode.id.indexOf("Page") != 0)) {
			return isShown(obj.parentNode);
		}
		return true;
	}
	return false;
}

function getPostfixForState(state) {
	if (state == 0) {
		return "_Normal";
	} else if (state == 16) {
		return "_Hovering";
	} else if (state == 32) {
		return "_Selected";
	} else if (state == 64) {
		return "_Disabled";
	} else if (state == 48) {
		return "_Selected_Hovering";
	} else if (state == 96) {
		return "_Selected_Disabled";
	}
	return "";
}

function getElementTypeById(id) {
	var index = id.indexOf("_");
	if (index == -1) {
		return "Unknown";
	} else {
		return id.substring(0, index);
	}
}

function changeState(id, state) {
	var obj = document.getElementById(id);
	if (obj != null) {
		var elemType = getElementTypeById(id);
		try {
			if (eval(elemType + "_changeState('" + id + "', " + state + ");") == true) {
				return;
			}
		} catch (err) {}
		var postfix = getPostfixForState(state);
		if (obj.tagName.toUpperCase() == "IMG") {
			obj.src = "images/" + id + postfix + ".png";
		} else {
			if (id.indexOf("TextBox") == 0) {
				obj = document.getElementById(id + "_Text");
			}
			obj.className = id + postfix;
			obj.checked = ((state & 32) > 0);
			if (obj.getAttribute("checked") != null) {
				obj.setAttribute("checked", ((state & 32) > 0));
			}
			obj.disabled = ((state & 64) > 0);
			try {
				eval(getElementTypeById(id) + "_onStateChanged('" + id + "', " + state + ");");
			} catch (err) {}
			try {
				var getExp = getElementTypeById(id) + "_getSelectedIndex('" + id + "');";
				var selIndex = eval(getExp);
				if (selIndex != -1) {
					var setExp = getElementTypeById(id) + "_setSelectedIndex('" + id + "', " + selIndex + ");";
					eval(setExp);
				}
			} catch (err) {}
		}
	}
}

function isBooleanTrue(b) {
	if (b == null) {
		return false;
	}
	if (typeof(b) == "boolean") {
		return b;
	}
	if (typeof(b) == "string") {
		return (b.toLowerCase() == "true") ? true : false;
	}
	return b != 0;
}

function isElementSelected(obj) {
	return isBooleanTrue(obj.getAttribute("checked"));
}

function isElementDisabled(obj) {
	return (typeof(obj.disabled) != 'undefined' && obj.disabled == true);
}

function getElementState(obj) {
	var state = 0;
	if (isElementSelected(obj)) {
		state |= 32;
	}
	if (isElementDisabled(obj)) {
		state |= 64;
	}
	return state;
}

function selectToggleObject(id, sel) {
	var obj = document.getElementById(id);
	if (obj != null) {
		if (obj.getAttribute("checked") != null) {
			var newSelected = (isBooleanTrue(obj.getAttribute("checked"))) ? false : true;
			if (sel == 0) {
				newSelected = false;
			} else if (sel == 1) {
				newSelected = true;
			}
			if (obj.getAttribute("checked") != null) {
				obj.setAttribute("checked", newSelected);
			}
			var postfix = getPostfixForState(getElementState(obj));
			obj.className = id + postfix;
		}
	}
}

function selectCheckBoxInGroup(id, sel, includeSiblings) {
	if (includeSiblings == true) {
		var targetElement = document.getElementById(id);
		if (targetElement != null) {
			var parentElement = targetElement.parentNode;
			if (parentElement.id.indexOf("Group") == 0) {
				var children = parentElement.childNodes;
				for(var i = 0; i < children.length; i ++) {
					if (typeof(children[i].id) != 'undefined' && children[i].id.indexOf("CheckBox") == 0) {
						selectToggleObject(children[i].id, sel);
					}
				}
			} else {
				selectToggleObject(id, sel);
			}
		}
	} else {
		selectToggleObject(id, sel);
	}
}

function selectRadioButtonInGroup(id, sel, unselectSiblings) {
	if (unselectSiblings == true) {
		var targetElement = document.getElementById(id);
		if (targetElement != null) {
			var parentElement = targetElement.parentNode;
			if (parentElement.id.indexOf("Group") == 0) {
				var children = parentElement.childNodes;
				for(var i = 0; i < children.length; i ++) {
					if (typeof(children[i].id) != 'undefined' && children[i].id.indexOf("RadioButton") == 0) {
						if (children[i].id == id) {
							selectToggleObject(children[i].id, sel);
						} else {
							selectToggleObject(children[i].id, 0);
						}
					}
				}
			} else {
				selectToggleObject(id, sel);
			}
		}
	} else {
		selectToggleObject(id, sel);
	}
}

function sleep(delay) {
	var start = new Date().getTime();
	while (new Date().getTime() < start + delay);
}

function clearTimeoutArray(arr) {
	if(arr.length){   
		for (var i = 0; i < arr.length; i ++) {
			clearTimeout(arr[i]);
		}
	}
}

function getEvent(){
	if(document.all) {
		return window.event;        
	}
	func=getEvent.caller;            
	while(func!=null){    
		var arg0=func.arguments[0];
		if(arg0){
			if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
				|| (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
				return arg0;
			}
		}
		func=func.caller;
	}
	return null;
}

function getEventSource() {
	var evt = getEvent();
	var element = evt.srcElement || evt.target;
	return element;
}

function getEventOffset(evt) {
	if(document.all) {
	    return evt;
	}
	var target = evt.target;
	var pageCoord = {
		x: getPosLeft(target),
		y: getPosTop(target)
	};
	var eventCoord = {
	    x: window.pageXOffset + evt.clientX,
	    y: window.pageYOffset + evt.clientY
	};
	var offset = {
	    offsetX: eventCoord.x - pageCoord.x,
	    offsetY: eventCoord.y - pageCoord.y
	};
	return offset;
}

function preLoadImg(url) {
	var img = new Image();
	img.src = url;
}

var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
}

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}

var BindAsEventListener = function(object, fun) {
	return function(event) {
		return fun.call(object, (event || window.event));
	}
}

function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

var DragEngine = Class.create();
DragEngine.prototype = {
	initialize: function(drag, draggableRect) {
		this.Drag = $(drag);
		this.DraggableRect = draggableRect;
		this._x = this._y = 0;
		this._fM = BindAsEventListener(this, this.Move);
		this._fS = Bind(this, this.Stop);
		this.Drag.style.position = "absolute";
		addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start));
	},
	Start: function(oEvent) {
		this._x = oEvent.clientX - this.Drag.offsetLeft;
		this._y = oEvent.clientY - this.Drag.offsetTop;
		if (this.DraggableRect == null || ptInRect(this._x, this._y, this.DraggableRect.x, this.DraggableRect.y, this.DraggableRect.w, this.DraggableRect.h)) {
			addEventHandler(document, "mousemove", this._fM);
			addEventHandler(document, "mouseup", this._fS);
			if (isIE) {
				addEventHandler(this.Drag, "losecapture", this._fS);
				this.Drag.setCapture();
			} else {
				addEventHandler(window, "blur", this._fS);
				oEvent.preventDefault();
			};
		}
	},
	Move: function(oEvent) {
		changeLocation(this.Drag.id, oEvent.clientX - this._x, oEvent.clientY - this._y, true);
	},
	Stop: function() {
		removeEventHandler(document, "mousemove", this._fM);
		removeEventHandler(document, "mouseup", this._fS);
		if (isIE) {
			removeEventHandler(this.Drag, "losecapture", this._fS);
			this.Drag.releaseCapture();
		} else {
			removeEventHandler(window, "blur", this._fS);
		};
	}
};

function setGlobalCursor(cursor) {
	document.body.style.cursor = cursor;
}

function changeSize(targetElementId, newWidth, newHeight) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		targetElement.style.width = newWidth;
		targetElement.style.height = newHeight;
		try {
			eval(getElementTypeById(targetElementId) + "_onResized('" + targetElementId + "');");
		} catch (err) {}
	}
}

function changeOpacity(targetElementId, opacity) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		var bgElement = document.getElementById(targetElementId + "_Bg");
		if (bgElement != null) {
			targetElement = bgElement;
		}
		targetElement.style.filter = "alpha(opacity=" + opacity + ")";
		targetElement.style.opacity = "" + (opacity / 100);
	}
}

function changeBgColor(targetElementId, bgColor) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		var bgElement = document.getElementById(targetElementId + "_Bg");
		if (bgElement != null) {
			targetElement = bgElement;
		}
		targetElement.style.backgroundColor = bgColor;
	}
}

function changeTextColor(targetElementId, textColor) {
	var targetElement = document.getElementById(targetElementId);
	if (targetElement != null) {
		var textElement = document.getElementById(targetElementId + "_Text");
		if (textElement != null) {
			targetElement = textElement;
		}
		targetElement.style.color = textColor;
	}
}

function setSelectedIndex(targetElementId, newIndex) {
	var prefix = getElementTypeById(targetElementId);
	try {
		eval(prefix + "_setSelectedIndex('" + targetElementId + "', " + newIndex + ");");
	} catch (err) {}
}

function isChildOf(childElement, parentElementId) {
	var obj = childElement;
	while (obj = obj.parentNode) {
		if (obj.id == parentElementId) {
			return true;
		}
	}
	return false;
}

function html2text(htmlStr) {
	var result = htmlStr.replace(/<[^>]+>/g, "");
	result = result.replace(/&amp;/g, "&");
	result = result.replace(/&lt;/g, "<");
	result = result.replace(/&gt;/g, ">");
	result = result.replace(/&nbsp;/g, " ");
	result = result.replace(/<br>/g, "\n");
	result = result.replace(/&apos;/g, "'");
	result = result.replace(/&quot;/g, "\"");
	return result;
}

function text2html(textStr) {
	var result = textStr.replace(/&/g, "&amp;");
	result = result.replace(/</g, "&lt;");
	result = result.replace(/>/g, "&gt;");
	result = result.replace(/ /g, "&nbsp;");
	result = result.replace(/\n/g, "<br>");
	result = result.replace(/'/g, "&apos;");
	result = result.replace(/"/g, "&quot;");
	return result;
}

function getCurrentPageIndex() {
	return realCurPageIndex + 1;
}

function getCurrentPageTitle() {
	var pageDiv = document.getElementById("Page" + getCurrentPageIndex());
	if (pageDiv != null) {
		return pageDiv.getAttribute("pageTitle");
	}
	return "";
}

function getScreenWidth() {
	return document.body.clientWidth;
}

function getScreenHeight() {
	return document.body.clientHeight;
}

function getCurrentYear() {
	return new Date().getFullYear();     
}

function getCurrentMonth() {
	return new Date().getMonth() + 1;     
}

function getCurrentDay() {
	return new Date().getDate();     
}

function getCurrentHours() {
	return new Date().getHours();     
}

function getCurrentMinutes() {
	return new Date().getMinutes();     
}

function getCurrentSeconds() {
	return new Date().getSeconds();     
}

function getCustomProperty(name) {
	var prop = customPropertyMap[name];
	if (prop != null) {
		return prop.value;
	}
	return null;
}

function setCustomProperty(name, value) {
	var prop = customPropertyMap[name];
	if (prop == null) {
		prop = new Object();
		prop.name = name;
		var tmpVal = parseInt(value, 10);
		if ("" + tmpVal == value) {
			prop.type = 0;
		} else {
			prop.type = 1;
		}
		customPropertyMap[name] = prop;
	}
	if (prop.type == 0) {
		if (typeof(value) != "number") {
			prop.value = parseInt(value, 10);
		} else {
			prop.value = value;
		}
	} else {
		if (typeof(value) != "string") {
			prop.value = "" + value;
		} else {
			prop.value = value;
		}
	}
}

function addCustomEventHandler(eventName, handler) {
	if (customEventHandlers[eventName] == null) {
		customEventHandlers[eventName] = new Array();
	}
	customEventHandlers[eventName].push(handler);
}

function triggerCustomEvent(eventName) {
	if (customEventHandlers[eventName] != null) {
		for (var i = 0; i < customEventHandlers[eventName].length; i ++) {
			customEventHandlers[eventName][i].call();
		}
	}
}

function getCurrentKeyCode() {
	var event = getEvent();
	if (event != null) {
		return event.keyCode;
	}
	return -1;
}

function getCtrlKeyState() {
	var event = getEvent();
	if (event != null) {
		return (event.ctrlKey == true) ? 1 : 0;
	}
	return 0;
}

function getAltKeyState() {
	var event = getEvent();
	if (event != null) {
		return (event.altKey == true) ? 1 : 0;
	}
	return 0;
}

function getShiftKeyState() {
	var event = getEvent();
	if (event != null) {
		return (event.shiftKey == true) ? 1 : 0;
	}
	return 0;
}

function disableSelection(target){
	if (typeof target.onselectstart!="undefined") {
		target.onselectstart=function(){return false};
	} else if (typeof target.style.MozUserSelect!="undefined") {
		target.style.MozUserSelect="none";
	} else {
		target.onmousedown=function(){return false};
	}
}

function saveFocus(elem, events) {
	for (var i = 0; i < events.length; i ++) {
		addHandler(elem, events[i], function(){focusedElementId = elem.id});
	}
}

function getFocusedElementId() {
	return focusedElementId;
}

function getCurrentCursorX() {
	var event = getEvent();
	if (event != null) {
		return event.clientX;
	}
	return -1;
}

function getCurrentCursorY() {
	var event = getEvent();
	if (event != null) {
		return event.clientY;
	}
	return -1;
}

function stopEventPropagation() {
	if (isIE) {
		window.event.cancelBubble = true;
	} else {
		getEvent().stopPropagation();
	}
	return false;
}